package city.spring.modules.medicine.controller;

import city.spring.modules.medicine.entity.MedicinesupplierEntity;
import city.spring.modules.medicine.service.MedicinesupplierService;
import city.spring.utils.MyBatisUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Controller：药品供应商信息表
 *
 * @author 谢金婷
 * @date 2020-08-03 09:48:00
 */
@RestController
@RequestMapping("/medicinesupplier")
public class MedicinesupplierController extends ApiController {
    private final MedicinesupplierService medicinesupplierService;
    /**
     * 实体类可排序字段
     */
    private final List<SFunction<MedicinesupplierEntity, ?>> entityOrderFields;

    public MedicinesupplierController(MedicinesupplierService medicinesupplierService) {
        this.medicinesupplierService = medicinesupplierService;
        entityOrderFields = new ArrayList<>();
        entityOrderFields.add(MedicinesupplierEntity::getId);
        entityOrderFields.add(MedicinesupplierEntity::getSuppliername);
        entityOrderFields.add(MedicinesupplierEntity::getType);
        entityOrderFields.add(MedicinesupplierEntity::getContact);
        entityOrderFields.add(MedicinesupplierEntity::getPhone);
        entityOrderFields.add(MedicinesupplierEntity::getAddress);
        entityOrderFields.add(MedicinesupplierEntity::getStatus);
        entityOrderFields.add(MedicinesupplierEntity::getDegree);
        entityOrderFields.add(MedicinesupplierEntity::getGmtCreate);
        entityOrderFields.add(MedicinesupplierEntity::getGmtModified);
    }

    /**
     * 获取全部的 <strong>药品供应商信息表</strong> 列表
     */
    @GetMapping("all")
    public Object listAll(@PageableDefault Pageable pageable, MedicinesupplierEntity entity) {
        LambdaQueryChainWrapper<MedicinesupplierEntity> lambdaQuery = buildLambdaQuery(entity);
        MyBatisUtils.lambdaQueryAddOrder(lambdaQuery, pageable, entityOrderFields);
        return success(lambdaQuery.list());
    }

    /**
     * 分页获取 <strong>药品供应商信息表</strong> 列表
     *
     * @param pageable 分页参数信息
     */
    @GetMapping
    public Object list(@PageableDefault Pageable pageable, MedicinesupplierEntity entity) {
        LambdaQueryChainWrapper<MedicinesupplierEntity> lambdaQuery = buildLambdaQuery(entity);
        MyBatisUtils.lambdaQueryAddOrder(lambdaQuery, pageable, entityOrderFields);
        Page<MedicinesupplierEntity> page = lambdaQuery.page(MyBatisUtils.toPage(pageable, false));
        return success(page);
    }

    /**
     * 获取一个 <strong>药品供应商信息表</strong>
     *
     * @param id 主键ID
     */
    @GetMapping("{id}")
    public Object info(@PathVariable String id) {
        return success(medicinesupplierService.getById(id));
    }

    /**
     * 添加一个 <strong>药品供应商信息表</strong>
     *
     * @param entity 修改后的信息
     */
    @PostMapping
    public Object add(@RequestBody MedicinesupplierEntity entity) {
        medicinesupplierService.saveMedicinesupplier(entity);

        HttpHeaders headers = new HttpHeaders();
        headers.add(HttpHeaders.LOCATION, String.valueOf(entity.getId()));
        return new ResponseEntity<>(headers, HttpStatus.CREATED);
    }

    /**
     * 修改一个 <strong>药品供应商信息表</strong>
     *
     * @param id     主键ID
     * @param entity 修改后的信息
     */
    @PutMapping("{id}")
    public Object update(@PathVariable String id, @RequestBody MedicinesupplierEntity entity) {
        entity.setId(id);
        medicinesupplierService.updateMedicinesupplier(entity);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    /**
     * 删除一个 <strong>药品供应商信息表</strong>
     *
     * @param id 主键ID
     */
    @DeleteMapping("{id}")
    public Object delete(@PathVariable String id) {
        medicinesupplierService.deleteMedicinesupplier(id);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 删除多个 <strong>药品供应商信息表</strong>
     *
     * @param ids 主键ID列表
     */
    @DeleteMapping
    public Object deleteIds(@RequestBody List<String> ids) {
        medicinesupplierService.deleteMedicinesupplier(ids);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 构建查询条件内容
     *
     * @param entity 实体对象
     * @return lambda query chain wrapper
     */
    private LambdaQueryChainWrapper<MedicinesupplierEntity> buildLambdaQuery(MedicinesupplierEntity entity) {
        LambdaQueryChainWrapper<MedicinesupplierEntity> lambdaQuery = medicinesupplierService.lambdaQuery();
        lambdaQuery.eq(entity.getId() != null, MedicinesupplierEntity::getId, entity.getId());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getSuppliername()), MedicinesupplierEntity::getSuppliername, entity.getSuppliername());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getType()), MedicinesupplierEntity::getType, entity.getType());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getContact()), MedicinesupplierEntity::getContact, entity.getContact());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getPhone()), MedicinesupplierEntity::getPhone, entity.getPhone());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getAddress()), MedicinesupplierEntity::getAddress, entity.getAddress());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getStatus()), MedicinesupplierEntity::getStatus, entity.getStatus());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getDegree()), MedicinesupplierEntity::getDegree, entity.getDegree());
        lambdaQuery.eq(entity.getGmtCreate() != null, MedicinesupplierEntity::getGmtCreate, entity.getGmtCreate());
        lambdaQuery.eq(entity.getGmtModified() != null, MedicinesupplierEntity::getGmtModified, entity.getGmtModified());
        return lambdaQuery;
    }

    /**
     * 构建查询条件内容
     *
     * @param entity 实体对象
     * @return lambda query chain wrapper
     */
    private QueryWrapper<MedicinesupplierEntity> buildQuery(MedicinesupplierEntity entity) {
        QueryWrapper<MedicinesupplierEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(entity.getId() != null, "id", entity.getId());
        wrapper.like(StringUtils.isNotBlank(entity.getSuppliername()), "suppliername", entity.getSuppliername());
        wrapper.like(StringUtils.isNotBlank(entity.getType()), "type", entity.getType());
        wrapper.like(StringUtils.isNotBlank(entity.getContact()), "contact", entity.getContact());
        wrapper.like(StringUtils.isNotBlank(entity.getPhone()), "phone", entity.getPhone());
        wrapper.like(StringUtils.isNotBlank(entity.getAddress()), "address", entity.getAddress());
        wrapper.like(StringUtils.isNotBlank(entity.getStatus()), "status", entity.getStatus());
        wrapper.like(StringUtils.isNotBlank(entity.getDegree()), "degree", entity.getDegree());
        wrapper.eq(entity.getGmtCreate() != null, "gmt_create", entity.getGmtCreate());
        wrapper.eq(entity.getGmtModified() != null, "gmt_modified", entity.getGmtModified());
        return wrapper;
    }

}
